package cn.hwadee.muying.controller;


import cn.hwadee.muying.commons.utils.DateTools;
import cn.hwadee.muying.dto.Worker;
import cn.hwadee.muying.entity.Job;
import cn.hwadee.muying.entity.WorkerLevel;
import cn.hwadee.muying.enums.Level;
import cn.hwadee.muying.mapper.PromotionLogMapper;
import cn.hwadee.muying.mapper.WorkerLevelMapper;
import cn.hwadee.muying.mapper.WorkerMapper;
import cn.hwadee.muying.service.*;
import cn.hwadee.muying.commons.constant.ConstantRole;
import cn.hwadee.muying.enums.Education;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import cn.hwadee.muying.commons.base.BaseController;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.ui.Model;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.hwadee.muying.entity.PromotionLog;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.shiro.authz.annotation.Logical;

/**
* <p>
* 晋升记录 前端控制器
*
* </p>
*
* @author Mankind
* @since 2018-08-06
*/
@Controller
@RequestMapping("/sys/promotionLog")
public class PromotionLogController extends BaseController {
    @Autowired
    private PromotionLogService promotionLogService;
    @Autowired
    private WorkerService workerService;

    @Autowired
    private UserService userService;

    @Autowired
    private WorkerLevelService workerLevelService;

    @Autowired
    private JobService jobService;

    @Autowired
    private WorkerLevelMapper workerLevelMapper;

    @ModelAttribute("worker")
    public Worker get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            Worker worker = workerService.selectById(id);
            if(worker != null){
                Job job=jobService.selectById(worker.getJob());
                worker.setJobName(job.getName());
                WorkerLevel workerLevel = workerLevelMapper.selectById(worker.getWorkerLevel().getValue());
                if(workerLevel.getHasNext()){
                    worker.setNextWorkerLevel(Level.getById((Integer.parseInt(workerLevel.getNextId().toString()))));
                }
            }
            return worker;
        }else{
            return new Worker();
        }
    }

    @RequiresPermissions("sys:promotionLog:list")
    @RequestMapping(value="/list",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        model.addAttribute("users",userService.selectAllByRole(ConstantRole.ROLE_NURSERY_TEACHER));
        model.addAttribute("preWorkerLevels", workerLevelService.selectAll());
        model.addAttribute("curWorkerLevels", workerLevelService.selectAll());
        return "/biz/promotionLog/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:promotionLog:list")
    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8")
    public GridResult list(Long userId,Long preWorkerLevelId,Long curWorkerLevelId,String keyword) throws Exception{
        MyPage<PromotionLog> page = getPage();
        page = promotionLogService.list(page,getCurrentUser(),userId,preWorkerLevelId,curWorkerLevelId,keyword);
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    /**
     * 跳转添加/编辑晋升记录界面
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:promotionLog:edit","sys:promotionLog:add"},logical=Logical.OR)
    @RequestMapping(value = {"/edit","/add"}, produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute Worker worker,Model model) throws Exception{
        model.addAttribute("worker",worker);
        return "/biz/promotionLog/edit";
    }

    /**
     * 处理更新晋升记录的请求
     * @param promotionLog
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:promotionLog:edit","sys:promotionLog:add"},logical=Logical.OR)
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute PromotionLog promotionLog){
        Map<String,Object> result = new HashMap();
        try {
        if(promotionLog.getId() != null && promotionLog.getId() > 0){
            promotionLogService.updateById(promotionLog);
        }else {
            promotionLog.setPromoteDate(new Date());
            Worker worker = workerService.selectById(promotionLog.getUserId());
            worker.setWorkerLevel(Level.getById(Integer.parseInt(promotionLog.getCurLevelId().toString())));
            workerService.updateById(worker);
            promotionLogService.insert(promotionLog);
        }
        result.put("status",200);
        result.put("msg","晋升记录信息保存成功");
        }catch (Exception e){
        e.printStackTrace();
        result.put("status",500);
        result.put("msg","晋升记录信息保存失败");
        }
        return result;
    }

    /**
     * 处理删除晋升记录的请求
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:promotionLog:delete")
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap();
        try {
        if(id != null && id > 0){
            promotionLogService.deleteById(id);
            result.put("status",200);
            result.put("msg","删除晋升记录成功");
        }else {
            result.put("status",500);
            result.put("msg","请选择删除的晋升记录");
        }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除晋升记录失败");
        }
        return result;
    }

}
